home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / pthd-0.000 / pthd-0 / pthd-0.7 / src_signals / sigaction.c.3 < prev    next >
Encoding:
Text File  |  1995-08-16  |  1.1 KB  |  68 lines

  1. /*
  2.  * sigaction.c.3
  3.  */
  4. #include <pthread.h>
  5. #include <stdio.h>
  6. #include "utils.h"
  7.  
  8. extern int getpid( void );
  9.  
  10. void
  11. ctrl_c_handler( int sig )
  12. {
  13.     print_str("^C interrupt.");
  14.     pthread_kill( NULL, SIGKILL );
  15. }
  16.  
  17. void
  18. term_handler( int sig )
  19. {
  20.     pthread_lock_global_np();
  21.     printf("Caught %d %s in handler!\n", sig, sys_signame[sig] );
  22.     pthread_unlock_global_np();
  23. }
  24.  
  25. void
  26. catcher( void )
  27. {
  28.     struct timespec ts = { 0, 50000 };
  29.  
  30.     while( 1 )
  31.     {
  32.         print_str("catcher");
  33.         pthread_delay_np( &ts );
  34.     }
  35.  
  36.     pthread_exit( (void *) 0 );
  37. }
  38.  
  39.  
  40. int main()
  41. {
  42.    pthread_t th;
  43.    struct sigaction act;
  44.    struct timespec ts = { 0, 100000 };
  45.  
  46.    act.sa_handler = term_handler;
  47.    sigemptyset( &act.sa_mask );
  48.    pthread_sigaction_np( SIGTERM, &act, NULL );
  49.  
  50.    act.sa_handler = ctrl_c_handler;
  51.    sigemptyset( &act.sa_mask );
  52.    pthread_sigaction_np( SIGINT, &act, NULL );
  53.  
  54.    pthread_create( &th, NULL, (thread_proc_t) catcher, NULL );
  55.  
  56.    /*
  57.     * Sleep for 0.5 seconds, wake-up, and send a signal to the
  58.     * catcher thread.  Do this until the process takes a kill -9.
  59.     */
  60.    while( 1 )
  61.    {
  62.        pthread_delay_np( &ts );
  63.        pthread_kill( th, SIGTERM );
  64.    }
  65.  
  66.    return(0);
  67. }
  68.